package com.panopset.op;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;

import org.joda.time.DateTime;

import com.panopset.compat.MathUtil;
import com.panopset.sf.FullScreenWindow;

/**
 * Play a tune (if not silent) and show screensaver when Alarm goes off. Default
 * warning is two minutes, just like football. Alarm will go off at the warning
 * interval prior to the alarm time.
 * 
 * @author Karl Dinwiddie
 * 
 */
public class Alarm {
	
	public class ColorVector {
		int c = MathUtil.random(0, 255);
		int v = MathUtil.random(1, 0) == 0 ? -1 : 1;
		public void incr() {
			c = c + v;
			if (c > 254) {
				v = -1;
			} else if (c < 1) {
				v = 1;
			}
		}
	}

	public static long TWO_MINUTE_DEFAULT_WARNING = 2 * 1000 * 60;
	private long warning = TWO_MINUTE_DEFAULT_WARNING;
	private String msg = "Ding ding ding-a-ling...";
	private int hr = 12;
	private int min = 0;
	private int x = 0;
	private int size = 1;
	public ColorVector[] cv = new ColorVector[] { new ColorVector(), new ColorVector(), new ColorVector()};
	boolean firstTime = true;

	/**
	 * 
	 * @param millis
	 *            Alarm actually goes off this number of milliseconds prior to
	 *            the alarm time. Default is two minutes.
	 */
	public void setWarning(long millis) {
		warning = millis;
	}

	public void setMessage(String message) {
		msg = message;
	}

	public void setHour(int hour) {
		hr = hour;
	}

	public void setMinute(int minute) {
		min = minute;
	}

	public void runInline() {
		isActive = true;
		DateTime target = new DateTime().withHourOfDay(hr)
				.withMinuteOfHour(min).withSecondOfMinute(0);
		FullScreenWindow fsw = null;
		while (isActive() && fsw == null) {
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				throw new RuntimeException();
			}
			DateTime now = new DateTime();
			if (now.isAfter(target.getMillis() - warning)) {
				fsw = new FullScreenWindow() {

					@Override
					public void paintCycle(Graphics2D gr) {
						if (firstTime) {
							firstTime = false;
							gr.setColor(Color.BLACK);
							gr.fillRect(0, 0, getWidth(), getHeight());
						}
						gr.setColor(new Color(cv[0].c,cv[1].c,cv[2].c));
						for (ColorVector x : cv) {
							x.incr();
						}
						Font font = new Font(Font.DIALOG, Font.BOLD, size++);
						gr.setFont(font);
						FontMetrics fm = gr.getFontMetrics();
						int msgWidth = fm.stringWidth(msg);
						int msgHeight = fm.getHeight();
						
						gr.drawString(msg, (getWidth()/2) - (msgWidth/2), getHeight() - (msgHeight / 2));
						
						if (x > getWidth() - msgWidth) {
							x = 0;
						}
						if (msgWidth > getWidth()) {
							size = 0;
						}
					}

					@Override
					public void quit() {
						// new PrompterSwing("asdf").get();
						super.quit();
					}

					@Override
					public void save() {
						super.save();
						stop();
					}
				};
				fsw.show();
			}
		}
	}
	public void run() {
		new Thread(new Runnable() {

			@Override
			public void run() {
				runInline();
			}

		}).start();
	}
	public Monitor m;
	
	public void setMonitor(Monitor monitor) {
		m = monitor;
	}
	private boolean isActive;
	public boolean isActive() {
		if (m == null) {
			return isActive;
		} else {
			if (m.isActive()) {
				return isActive;
			}
			return false;
		}
	}
	public void stop() {
		isActive = false;
	}
}
